package 链表题.合并有序链表;

import 链表题.ListNode;

/**
 * 方法一:
 */

public class Test1 {

    public static ListNode mergeTwoLists(ListNode p1, ListNode p2) {
        ListNode s = new ListNode(-1, null);
        ListNode p = s;
        while (p1 != null && p2 != null) {
            if (p1.val < p2.val) {
                p.next = p1;
                p = p.next;
                p1 = p1.next;
            } else {
                p.next = p2;
                p = p.next;
                p2 = p2.next;
            }
        }
        p.next = p1 == null ? p2 : p1;
        return s.next;
    }

    public static void main(String[] args) {
        ListNode n1 = ListNode.of(1, 2, 3, 5, 6, 9, 10);
        ListNode n2 = ListNode.of(0, 4, 7, 8);
        System.out.println(n1);
        System.out.println(n2);
        ListNode n = mergeTwoLists(n1, n2);
        System.out.println(n);
    }

}
